﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKArch.Utils;
using HYView;

namespace YJKArch.DimensionModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdWallCenterDim : YJKCommandManual
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)11;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			ViewPlan viewPlan = cmdData.Application.ActiveUIDocument.ActiveView as Autodesk.Revit.DB.ViewPlan;
			if (viewPlan.SketchPlane == null)
			{
				Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(viewPlan.Origin, viewPlan.RightDirection, viewPlan.UpDirection);
				Transaction transaction = new Transaction(cmdData.Application.ActiveUIDocument.Document);
				transaction.Start("new sketchPlane");
				SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(cmdData.Application.ActiveUIDocument.Document, plane);
				viewPlan.SketchPlane = sketchPlane;
				transaction.Commit();
			}
			if (CmdWallCenterDim._hWndRevit == null)
			{
				CmdWallCenterDim._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			List<DimensionType> dimensionTypes = Common.GetDimensionTypes(this.m_doc);
			dimensionTypes.Sort(new DimensionTypeComparer());
			List<DimensionType> list = new List<DimensionType>();
			List<string> list2 = new List<string>();
			foreach (DimensionType dimensionType in dimensionTypes)
			{
				DimensionType dimensionType2 = dimensionType as DimensionType;
				if (dimensionType2.ParametersMap != null && dimensionType2.ParametersMap.Size != 0 && (int)dimensionType2.StyleType == 0)
				{
					list.Add(dimensionType2);
					list2.Add(dimensionType2.Name);
				}
			}
			WallCenterDimensionForm wallCenterDimensionForm = null;
			Result result;
			try
			{
				wallCenterDimensionForm = new WallCenterDimensionForm(list2);
				wallCenterDimensionForm.Show(CmdWallCenterDim._hWndRevit);
				int wMsg = 7;
				CmdWallCenterDim.SendMessage(CmdWallCenterDim._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				DimensionCreator dimensionCreator = new DimensionCreator(this.m_uiApp);
				while (dimensionCreator.WallCenterDim(wallCenterDimensionForm, list, ref message) != Result.Cancelled)
				{
				}
				if (wallCenterDimensionForm != null)
				{
					wallCenterDimensionForm.Close();
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message += ex.ToString();
				if (wallCenterDimensionForm != null)
				{
					wallCenterDimensionForm.Close();
				}
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public static WindowHandle _hWndRevit;
	}
}
